/**
 * Licensed under the Apache License Version 2.0.
 *
import java.io.File;
rg/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.client.java;

import java.io.File;

/**
 *
 */
public class JavaSourceWriterUtils
{
	public static String deriveGetterName(String propertyName)
	{
		return "get" + String.valueOf(propertyName.charAt(0)).toUpperCase() + propertyName.substring(1);
	}

	public static String deriveSetterName(String propertyName)
	{
		return "set" + String.valueOf(propertyName.charAt(0)).toUpperCase() + propertyName.substring(1);
	}

	public static String deriveQueryMethodName(String propertyName)
	{
		return "is" + String.valueOf(propertyName.charAt(0)).toUpperCase() + propertyName.substring(1);
	}

	public static String toJavaType(String schemaType)
	{
		if("boolean".equals(schemaType)) return "java.lang.Boolean";
		if("byte".equals(schemaType)) return "java.lang.Byte";
		if("short".equals(schemaType)) return "java.lang.Short";
		if("char".equals(schemaType)) return "java.lang.Character";
		if("int".equals(schemaType)) return "java.lang.Integer";
		if("float".equals(schemaType)) return "java.lang.Float";
		if("long".equals(schemaType)) return "java.lang.Long";
		if("double".equals(schemaType)) return "java.lang.Double";
		if("string".equals(schemaType)) return "java.lang.String";
		if("date".equals(schemaType)) return "java.util.Date";
		if("datetime".equals(schemaType)) return "java.sql.Timestamp";
		if("byteStream".equals(schemaType)) return "java.io.InputStream";
		if("charStream".equals(schemaType)) return "java.io.Reader";

		return schemaType;
	}

	public static String fieldDeclarationSource(String pname, String ptype)
	{
		StringBuilder sb = new StringBuilder();

		sb.append("\t" + "private " + ptype + " " + pname + ";");

		return sb.toString();
	}

	public static String getterSource(String pname, String ptype)
	{
		StringBuilder sb = new StringBuilder();

		String getter = JavaSourceWriterUtils.deriveGetterName(pname);

		sb.append("\t" + "/** Getter for parameter " + pname + "*/").append("\n");
		sb.append("\t" + "public " + ptype + " " + getter + "()").append("\n");
		sb.append("\t" + "{").append("\n");
		sb.append("\t" + "\t" + "return this." + pname + ";").append("\n");
		sb.append("\t" + "}");

		return sb.toString();
	}

	public static String setterSource(String pname, String ptype)
	{
		StringBuilder sb = new StringBuilder();

		String setter = JavaSourceWriterUtils.deriveSetterName(pname);

		sb.append("\t" + "/** Setter for parameter " + pname + "*/").append("\n");
		sb.append("\t" + "public void " + setter + "(" + ptype + " " + pname + ")").append("\n");
		sb.append("\t" + "{").append("\n");
		sb.append("\t" + "\t" + "this." + pname + " = " + pname + ";").append("\n");
		sb.append("\t" + "}");

		return sb.toString();
	}

	public static String unqualifiedClassName(String fqClassName)
	{
		int lastDot = fqClassName.lastIndexOf('.');

		return lastDot > -1 ? fqClassName.substring(lastDot + 1) : fqClassName;
	}

	public static String packageName(String fqClassName)
	{
		int lastDot = fqClassName.lastIndexOf('.');

		return lastDot > -1 ? fqClassName.substring(0, lastDot) : "";
	}

	public static File packageSourceDir(File rootSrcDir, String rootPackageName, String fqClassName)
	{
		String pkg = packageName(fqClassName);

		File packageSrcDir = null;

		File rootPackageDir = null;

		if(rootSrcDir != null && rootPackageName != null)
		{
			rootPackageDir = new File(rootSrcDir, rootPackageName.replace('.', '/'));
		}
		else if(rootSrcDir == null && rootPackageName != null)
		{
			rootPackageDir = new File(rootPackageName.replace('.', '/'));
		}
		else if(rootSrcDir != null && rootPackageName == null)
		{
			rootPackageDir = rootSrcDir;
		}
		else
		{
			rootPackageDir = new File(".");
		}

		packageSrcDir = new File(rootPackageDir, pkg.replace('.', '/'));

		return packageSrcDir;
	}

	/**
	 * Clean all occurrences of white-spaces (newline, carriage return, tabs and
	 * spaces) from the given string.
	 *
	 * @param str
	 *            The input string that needs to be cleaned of white-spaces
	 * @return String representing the input str without the white-space
	 *         characters.
	 */
	public static String cleanWhiteSpaces(String str)
	{
		return str.trim().replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').replaceAll(" ", "");
	}

	/**
	 * Create a valid Java variable name from the input string.
	 *
	 * @param str
	 *            The input string from which the Java variable needs to be
	 *            created.
	 * @return A valid Java variable name created from str
	 */
	public static String createValidVariableName(String str)
	{
		final char[] junkChars = {
			'.', '-', '$', '~', '`', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+',
			'{', '}', '[',  ']', '|', '\\', '/', '?', '<', '>', ',', ':', ';', '"', '\''
		};

		str = cleanWhiteSpaces(str);

		for(char junkChar : junkChars)
		{
			str = str.replace(junkChar, '_');
		}

		return str;
	}
}
